﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Huayuan.Manage.Common;
using Hyt.Core.Interceptors;
using Hyt.Passport;
using Hyt.Passport.Service;
using Hyt.Web;
using Hyt.Web.Security;
using Hyt.Web.Mvc;

namespace Huayuan.Manage.Controllers
{
    [AuthInterceptor]
    public class LoginController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        public virtual JsonResult Auth(string username,string password,string safecode)
        {
            //this.RegisterExceptionHandler(exp => Const.JsonError(exp.Message));
            using (var ufw = new WebUnitOfWork())
            {
                //验证安全码
                if (!ufw.ServiceCenter.ValidSafeCode(Session.SessionID, safecode))
                {
                    ufw.ServiceCenter.ClearSafeCode(Session.SessionID);
                    throw new Exception("验证码错误");
                }
                //登录
                var administrator = ufw.ServiceCenter.AdministratorLogin(username, password, HttpHelper.GetIP(System.Web.HttpContext.Current));
                
                //保存登录标志,Cookie
                ufw.SetPrincipal(administrator);
                ufw.CookiePassportManager.SavePrincipal(System.Web.HttpContext.Current, administrator);
                ufw.Commit();
            }
            return Const.JsonSuccess;
        }

    }
}
